home *** CD-ROM | disk | FTP | other *** search
- Path: newsfeed.internetmci.com!panix!not-for-mail
- From: acinader@panix.com (Arthur Cinader Jr)
- Newsgroups: comp.lang.c++,gnu.g++.help
- Subject: Re: DONT UNDERSTAND - array of char error
- Date: 3 Mar 1996 09:50:22 -0500
- Organization: Panix
- Message-ID: <4hcbje$h52@panix.com>
- References: <4habvu$99j@panix.com> <4hahju$bil@news.BelWue.DE>
- NNTP-Posting-Host: panix.com
-
- In <4hahju$bil@news.BelWue.DE> kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl) writes:
-
- Dietmar,
-
- Wow, thanks. I love the internet and thank you for your kind
- assistance!
-
- >Well, because you forgot about an important restriction on arrays: All
- >but the first dimension has to be fixed at compile-time.
-
- >: In the definition (.C) file, I use the comand:
-
- >: labels = new char[maxargs][fieldlength]; // make label array
- >: params = new char[maxargs][fieldlength]; // make params array
-
- >... i.e. 'fieldlength' has to be a 'const'. If 'fieldlength' is
- >non-const you have to allocate the second dimension by hand (unless you
- >use an array class like suggested below):
-
- I thought I covered this by making
-
- const int fieldlength;
-
- and using a member intializer like this:
-
- Param::Param(char *shape)
- :maxargs(10), fieldlength(20)
-
- In any event, what you showed me below is what I wanted to
- do, but couldn't come up with on my own...
-
- > lables = new char *[maxargs]; // it is valid to allocate an array
- > params = new char *[maxargs]; // of 'char*'
- > for (int i = 0; i < maxargs; i++)
- > {
- > labels[i] = new char[fieldlength]; // ... and to allocate an
- > params[i] = new char[fieldlength]; // of 'char'.
- > }
-
- >... but an array thus create has to be destructed like this:
-
- > for (int i = maxargs; i--; )
- > {
- > delete[] params[i];
- > delete[] lables[i];
- > }
- > delete params;
- > delete lables;
-
- >Which is, IMHO, far to dangerous to be done: Given that you run out of
- >memory, you are likely to end up with a serious memory-leak. I know how
- >to program in C++ and I won't do it like this... This is exactly the
- >problem I have with all C++ tutorials I have seen until know: They
- >introduce the low-level methods, requiring to be an expert to handle
- >them correctly, but they don't introduce the clean methods provided by
- >the (upcoming) ISO Standard C++ Library (to be fair: this library was
- >introduced into the standard quite recently, i.e. sometime in 1994). A
- >clean method is to use the 'vector' class found in libg++ starting with
- >libg++-2.6.? (you didn't mention what version you are using but I
- >assume a recent one, e.g. 2.7.2):
-
-
- Your point noted, but I am going to risk it in this case. I
- am overwhelmed enough without having to stop now to figure out
- how to use the libraries and understand their interface. I do
- recognize this as a crucial part of my becoming productive and
- I will endevour to find documentation on the ISO standard
- library and learn about how to use it. FWIW the next chapter
- in the text is on I/O streams which I believe spends a
- considerable amount of time explaining "pre-packaged" class
- hiararchies and how to navigate them.
-
-
- Thanks again Dietmar.
-
- Best Regards,
-
- Arthur
-